﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using JOIN.Rights.Model;
using JOIN.Rights.BLL;

namespace JOIN.Rights.Web.Apps
{
    /// <summary>
    /// Summary description for tree
    /// </summary>
    public class tree : IHttpHandler
    {
        public item item=new item();

        public void ProcessRequest(HttpContext context)
        {
            /*
            item = new Apps.item() { id="0",text ="test0"};
            item.items.Add(new Apps.item() { id="1",text="test1" });

            XmlSerializer xml = new XmlSerializer(this.GetType());
            MemoryStream ms = new MemoryStream();
            xml.Serialize(ms, this);
            string xmlstring= System.Text.Encoding.UTF8.GetString(ms.ToArray());
            //*/
            //string test = @"";
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(menuXml);
        }
        string _menuXml = "<tree id=\"0\">{0}</tree>";
        string _menuItem = "<item id=\"{1}\" text=\"{2}\">{0}</item>";
        string xmlItem = "";
        public string menuXml
        {
            get 
            {
                MenusInfo menu = new MenusInfo() { ProjectID = "rights", MenuParent = "" };
                new MenusBLL(menu).Load(menu, menu.ProjectID, menu.MenuParent);
                foreach (MenusInfo item in menu.SubMenu)
                {
                    xmlItem+=this.loadItem(item);
                }
                return string.Format(_menuXml,xmlItem);
            }
           
        }

        private string loadItem(MenusInfo menu)
        {
            string result = "";
            if (menu.SubMenu.Count > 0)
            {
                foreach (MenusInfo item in menu.SubMenu)
                {
                    loadItem(item);
                }
            }
            result = string.Format(_menuItem, result, menu.MenuID, menu.MenuDesc);
            return result;
        }
        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string test
        {
            get { return "<tree id=\"0\" radio=\"1\">"+
	"<item   text=\"Books\" id=\"books\" im0=\"tombs.gif\" im1=\"tombs_open.gif\" im2=\"tombs.gif\" >"+
		"<item text=\"Mystery &amp; Thrillers\" id=\"mystery\" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">"+
			"<item text=\"Lawrence Block\" id=\"lb\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\">"+
			"	<item text=\"All the Flowers Are Dying\" id=\"lb_1\" im0=\"iconText.gif\" im1=\"book_titel.gif\" im2=\"book_titel.gif\"/>"+
			"	<item text=\"The Burglar on the Prowl\" id=\"lb_2\" im0=\"iconText.gif\" im1=\"book_titel.gif\" im2=\"book_titel.gif\"/>"+
			"	<item text=\"The Plot Thickens\" id=\"lb_3\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
			"	<item text=\"Grifter's Game\" id=\"lb_4\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
            "	<item text=\"The Burglar Who Thought He Was Bogart\" id=\"lb_5\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>" +
			"</item>"+
			"<item text=\"Robert Crais\" id=\"rc\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\">"+
			"	<item text=\"The Forgotten Man\" id=\"rc_1\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
			"	<item text=\"Stalking the Angel\" id=\"rc_2\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
			"	<item text=\"Free Fall\" id=\"rc_3\" im0=\"iconText.gif\" im1=\"book_titel.gif\" im2=\"iconText.gif\"/>"+
			"	<item text=\"Sunset Express\" id=\"rc_4\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
			"	<item text=\"Hostage\" id=\"rc_5\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
			"</item>"+
			"<item text=\"Ian Rankin\" id=\"ir\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
			"<item text=\"James Patterson\" id=\"jp\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
			"<item text=\"Nancy Atherton\" id=\"na\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"	</item>"+
	"	<item text=\"History\" id=\"history\" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">"+
	"		<item text=\"John Mack Faragher\" id=\"jmf\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"		<item text=\"Jim Dwyer\" id=\"jd\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"		<item text=\"Larry Schweikart\" id=\"ls\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"		<item text=\"R. Lee Ermey\" id=\"rle\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"	</item>"+
	"	<item text=\"Horror\" id=\"horror\" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">"+
	"		<item text=\"Stephen King\" id=\"sk\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"		<item text=\"Dan Brown\" open=\"1\" id=\"db\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\">"+
	"			<item text=\"Angels &amp; Demons\" id=\"db_1\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
	"			<item text=\"Deception Point\" id=\"db_2\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
	"			<item text=\"Digital Fortress\" id=\"db_3\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
	"			<item text=\"The Da Vinci Code\" id=\"db_4\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
	"			<item text=\"Deception Point\" id=\"db_5\" im0=\"iconText.gif\" im1=\"iconText.gif\" im2=\"iconText.gif\"/>"+
	"		</item>"+
	"		<item text=\"Mary Janice Davidson\" id=\"mjd\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"		<item text=\"Katie Macalister\" id=\"km\" im0=\"books_close.gif\" im1=\"books_open.gif\" im2=\"books_close.gif\"/>"+
	"	</item>"+		
"</item></tree>";
}
        }
    }

    public class item
    {
        public string text
        {
            get;
            set;
        }
        public string id
        {
            get;
            set;
        }

        public string im0
        {
            get;
            set;
        }
        public string im1
        {
            get;
            set;
        }
        public string im2
        {
            get;
            set;
        }

        List<item> _items;
        public List<item> items
        {
            get {
                if (_items == null) {
                    return new List<item>();
                }
                return _items;
            }
            set { _items = value; }
        }

    }
}